﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.Contracts.InterFaces;

using System.IO;
using WCFMINERP.Contracts.DataContracts;
using System.ServiceModel;
using WCFMINERP.ContextPropagation;

namespace WCFMINERP.Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ITransferFile _proxy;
        private void button1_Click(object sender, EventArgs e)
        {
            NetTcpBinding binding = new NetTcpBinding();
            binding.TransferMode = TransferMode.Streamed;
            binding.SendTimeout = new TimeSpan(0, 30, 0);
            //利用通道创建客户端代理
            _proxy = ChannelFactory<ITransferFile>.CreateChannel(binding, new EndpointAddress("net.tcp://192.168.0.121:8899/WCFMINERP/Services/TransferFileServers/TransferFilesService"));
            IContextChannel obj = _proxy as IContextChannel;
            //_proxy.InitTransferFile("0023201107110001", 447180800);

            //WCFMINERP.ContextPropagation.ApplicationContext.Current.AlphaPara = new ApplationParas();
            ////_proxy.InitTransferFile("0023201107110002", 447180830);
            ////_proxy
            //var list = _proxy.GetShopFileList();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        string FilePath;
        private void button3_Click(object sender, EventArgs e)
        {
            //if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Desktop;
            //    textBox1.Text = FilePath = folderBrowserDialog1.SelectedPath;
            //}
            OpenFileDialog dia = new OpenFileDialog();
            if (dia.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = FilePath = dia.FileName;
                
            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int maxSiz = 1024 * 100;  //设置每次传100k                              
            FileStream stream = System.IO.File.OpenRead(FilePath);    //读取本地文件
            string fileName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1);
            TransferFileInfo file = _proxy.GetFile(fileName);   //更加文件名,查询服务中是否存在该文件
            if (file==null)     //表示文件不存在
            {
                //file = new TransferFileInfo();
                file.Offset = 0; //设置文件从开始位置进行数据传递
            }
            file.Name = fileName;
            file.Length = stream.Length;
            if (file.Length == file.Offset) //如果文件的长度等于文件的偏移量，说明文件已经上传完成
            {
                MessageBox.Show("该文件已经在服务器中,不用上传！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                while (file.Length != file.Offset)  //循环的读取文件,上传，直到文件的长度等于文件的偏移量
                {
                    file.Data = new byte[file.Length - file.Offset <= maxSiz ? file.Length - file.Offset : maxSiz]; //设置传递的数据的大小
                    stream.Position = file.Offset; //设置本地文件数据的读取位置
                    stream.Read(file.Data, 0, file.Data.Length);//把数据写入到file.Data中
                    //要捕捉连接不上的异常
                    file = _proxy.UplodaFile(file);     //上传

                   long fileoffset = file.Offset;
                   label1.Text = fileoffset + "/" + file.Length;
                }

            }
            stream.Close();
        }
    }
}
